<?php

namespace App\Admin\Controllers;

use App\Models\Lock;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
// use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use App\Models\ProjectUser;
use App\Models\Project;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use App\Handlers\AliDeviceHandler;

class LockController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('锁列表')
            ->description()
            ->body($this->grid());
    }

    protected function grid()
    {
        return Grid::make(new Lock(), function (Grid $grid) {
            $grid->column('name', '名称')->sortable();
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId')->toArray();
            $grid->column('project_id', '所属项目')->using($res);
            $grid->column('deviceName','阿里云设备名称');
            // $grid->column('mac','mac地址');
            $grid->column('qrcode','二维码')->image(100,100);
            // $grid->column('qrcode','二维码下载')->downloadable();
            $grid->column('id','开锁记录')->display(function ($id) {
                $recordUrl = url('admin/lock-records').'/'.$id;
                return '<a href="'.$recordUrl.'">查看记录</a>';

            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        });
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建锁')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new Lock(), function (Form $form) {
            $form->text('name', '名称')->required();
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId');
            $form->select('project_id', '所属项目')->options($res)->required();
            // $form->text('productKey', '阿里云产品key')->required();
            $form->text('deviceName', '阿里云设备名称')->required();
            // $form->text('mac', 'mac地址')->required();
            $form->hidden('qrcode');
            $form->hidden('productKey');

            $form->saving(function (Form $form) {
                $qrcode = Lock::genQrcode($form->deviceName);
                $form->qrcode = $qrcode;
                $form->productKey = 'a1biYnlmEmv';
                $boxId = Lock::where('deviceName', $form->deviceName)->first();;
                if ($boxId) {
                    return $form->response()->error('阿里云设备名称已存在');
                }
            });
            $form->saved(function (Form $form, $result) {
                // Cabinet::syncWeikong($form->getKey());
                AliDeviceHandler::registerDevice($form->deviceName,2);
            });
            $form->deleted(function (Form $form, $result) {
                if ($result) {
                    $data = $form->model()->toArray();
                    Lock::delQrcode($data[0]['deviceName']);
                    AliDeviceHandler::deleteDevice($data[0]['deviceName'],2);
                }
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('锁编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }

    public function show($id){
        return Show::make($id, new Lock(), function (Show $show) {
            $show->field('name','名称');
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId')->toArray();
            $show->field('project_id','所属项目')->using($res);
            $show->field('deviceName', '阿里云设备名称')->required();
            // $show->field('mac','mac地址');
            $show->field('qrcode','二维码')->image();
        });
    }
}